package com.ce.pms.occupation.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.occupation.dao.IPerfectItemDispatchDao;
import com.ce.pms.occupation.model.PerfectItemDispatchDO;
import com.ce.pms.occupation.vo.PerItemDispSearchVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
/**
 * 功能描述: 整改项目派工DAO实现类
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author  胡煦
 * Create on:2008-12-31
 * Edit author:
 * Edit on:
 * 修改原因：

 */

public class PerfectItemDispatchDaoImpl extends PmsBaseDaoImpl<PerfectItemDispatchDO> implements
		IPerfectItemDispatchDao {
	/**
	 * 方法描述: 分页查询所有整改项目派工
	 *
	 * @author 胡煦 Create on:2008-12-29
	 * @param perItemDispSearchVO
	 * @return 返回当前页的查询结果 Edit author: Edit on: 修改原因：
	 */
	public Page queryPerfectItemDispatchDOByPage(PerItemDispSearchVO perItemDispSearchVO) {

		StringBuilder sb = null;
		List<Object> paramList = new ArrayList<Object>();
		int i = 0;
		// 房间ID
		if (!BlankUtil.isBlank(perItemDispSearchVO.getHouseId())) {
			sb = new StringBuilder("select distinct pd from PerfectItemDispatchDO pd left join pd.perfectItemRequisitions pr where 1 = 1");
			sb
					.append(" and pr.inspectionRequisition.identityValidate.house.keyword = ?");
			paramList.add(i++, perItemDispSearchVO.getHouseId());
		}
		//承包商设置Id
		if (!BlankUtil.isBlank(perItemDispSearchVO.getContractorInfoId())&&BlankUtil.isBlank(perItemDispSearchVO.getHouseId())) {
			sb = new StringBuilder("from PerfectItemDispatchDO pd where 1 = 1 ");
			sb
					.append(" and pd.contractorInfoConfig.keyword = ?");
			paramList.add(i++, perItemDispSearchVO.getContractorInfoId());
		}
		if (BlankUtil.isBlank(perItemDispSearchVO.getContractorInfoId())&&BlankUtil.isBlank(perItemDispSearchVO.getHouseId())) {
			sb = new StringBuilder("from PerfectItemDispatchDO pd where 1 = 1 ");
		}
		if (!BlankUtil.isBlank(perItemDispSearchVO.getOrderKey())
				&& !BlankUtil.isBlank(perItemDispSearchVO.getOrderValue())) {
			sb.append(" order by pd.").append(perItemDispSearchVO.getOrderValue()).append(
					" ").append(perItemDispSearchVO.getOrderKey());

		} else {
			sb
					.append(" order by pd.dispatchDate desc,pd.id");
		}

		return super.pagedQueryByPageNo(sb.toString(), perItemDispSearchVO.getPageNo(), perItemDispSearchVO.getPageSize(),paramList.toArray());
	}

	/**
	 * 功能描述:获取当天最大的整改项目派工单号<br>
	 * @return<br>
	 * Create author:胡煦<br>
	 * Create on:2009-1-4<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String getMaxDispatchNumber() {
		StringBuilder sql= new StringBuilder("select DISPATCH_NUMBER from OM_PERFECT_ITEM_DISPATCH rm ").
		append(" where rm.PERFECT_ITEM_DISPATCH_ID = (select max(`PERFECT_ITEM_DISPATCH_ID`) from OM_PERFECT_ITEM_DISPATCH)");
		Query query = super.createSQLQuery(sql.toString(),null);
		Object obj = query.uniqueResult();
		if(BlankUtil.isBlank(obj))
			return null;
		String number = (String) obj;
		return number;
	}
}
